package com.china08.yxyapi.api.manage.sch.office.innernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DefaultImageUtil;

/**
 * 学校管理员 收件箱 未读列表查询
 * 
 */
@CrossOrigin
@RestController("SchInnerNoticeReceiveNoReadSrhController")
public class InnerNoticeReceiveNoReadSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/innerNotice/receive/noRead", method = RequestMethod.POST)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "created_date");
        // 当前教育局id
        String schoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        // 未删除状态
        int isDelete = 0;

        List<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiver(schoolId, userId, isDelete, 0, pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;
        for (YxyNoticeInner yxyNoticeInner : page) {
            respModel = new RespModel();
            String avatar = yxyNoticeInner.getAvatar();
            avatar = DefaultImageUtil.default1(avatar);
            yxyNoticeInner.setAvatar(avatar);
            BeanUtils.copyProperties(yxyNoticeInner, respModel);
            respModel.setCreatedDate(DateToolUtils.format(yxyNoticeInner.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD));
            // JAVA 8 判断查询接受用户信息
            respModel.setIsRead(0);
            respModel.setTitle(yxyNoticeInner.getTitle());
            respModelLst.add(respModel);
        }
        int totalElements = yxyNoticeInnerRepository.countBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead(schoolId, userId, isDelete, 0);
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        private String id;
        // 消息标题
        private String title;
        // 发件人id
        private String userId;
        // 发件人名称
        private String userNick;
        // 发布对象 教育局id
        private String schoolId;
        // 发布对象 教育局名称
        private String schoolNick;
        // 是否已读 0:未读 1:已读
        private int isRead;
        /** The content. 内容 */
        private String content;

        private String avatar;
        /** The adjunct. 附件的文件路径,附件原始文件名称。 */
        private String attachmentName;

        private String attachmentPath;
        /**
         * 图片附件,可以放置多张图片
         */
        private List<String> image;
        private String createdDate;

        public String getCreatedDate() {
            return createdDate;
        }

        public void setCreatedDate(String createdDate) {
            this.createdDate = createdDate;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserNick() {
            return userNick;
        }

        public void setUserNick(String userNick) {
            this.userNick = userNick;
        }

        public String getSchoolId() {
            return schoolId;
        }

        public void setSchoolId(String schoolId) {
            this.schoolId = schoolId;
        }

        public String getSchoolNick() {
            return schoolNick;
        }

        public void setSchoolNick(String schoolNick) {
            this.schoolNick = schoolNick;
        }

        public int getIsRead() {
            return isRead;
        }

        public void setIsRead(int isRead) {
            this.isRead = isRead;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public String getAttachmentName() {
            return attachmentName;
        }

        public void setAttachmentName(String attachmentName) {
            this.attachmentName = attachmentName;
        }

        public String getAttachmentPath() {
            return attachmentPath;
        }

        public void setAttachmentPath(String attachmentPath) {
            this.attachmentPath = attachmentPath;
        }

        public List<String> getImage() {
            return image;
        }

        public void setImage(List<String> image) {
            this.image = image;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

    }
}
